GBML manual / Strings

string_repeat

text written out times times in a row - string_repeat("ab", 3) is "ababab". A string has only so much room: if the result would not fit, it is quietly cut off at the end, so string_repeat("ab", 1000) is one full string of "abab...", not a thousand of anything.

Syntax

string_repeat(text, times) -> string

Arguments

NameType
textstring
timesint

Returns

string

Description

text written out times times in a row: string_repeat("ab", 3) is "ababab". A string only has so much room, so if the answer would not fit, it is cut off at the end - no error, and the game keeps running. string_repeat("ab", 1000) does not give you a thousand of anything: it gives you one full string of abab..., as many letters as the string has room for (32 characters, unless you changed your project's Longest string setting).

A times of 0 or less gives empty text, and so does repeating empty text. string_repeat stops the moment the room is full, so asking for a huge times costs no more than asking for a small one.

Example

var hp = 3;
var bar = string_repeat("#", hp);            // "###"
var cheer = string_repeat("ha", 3);          // "hahaha"
var empty = string_repeat("ha", 0);         // ""
var too_much = string_repeat("ab", 1000);    // as many "abab..." as fit
var was_cut = string_length(too_much) < 2000;

To find out whether the answer was cut, compare its length with the length you expected: string_length(too_much) here is the string's room, not 2000.

If you keep the answer in a variable that you gave a smaller room with /// @capacity, and the answer is longer than that room, the game stops with an error screen when the line runs (a build with the safety checks on, which is the default). The cutting happens inside string_repeat, at your project's Longest string size - not at the size of the variable that receives the answer. See Text for what to do about it.

On the GBA

A string has a fixed amount of room - your project's Longest string setting (32 characters unless you changed it). string_repeat fills that room and stops: no error, no crash, the game carries on with the part that fit. times of 0 or less, or an empty text, gives empty text. It stops as soon as the room is full, so a huge times costs no more than a small one. Call string_length on the result to find out whether it was cut.

See also: string_joinstring_lengthstring_insert